<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2016/3/9
 * Time: 11:11
 */
namespace Chealth\Model;
use Think\Model;
class ChealthTimeModel extends Model{

    protected $_validate = array(
        array('name','require','预约项目名称必须填写!', self::MUST_VALIDATE),
        array('reserve_start_time','require','请填写预约开始时间!', self::MUST_VALIDATE),
        array('reserve_end_time','require','请填写预约结束时间!', self::MUST_VALIDATE),
        array('reserve_end_time','_validateReserveEndTime','预约结束时间不能小于预约开始时间!', self::EXISTS_VALIDATE, 'callback'),
        array('check_start_time','require','请填写查体开始时间!', self::MUST_VALIDATE),
        array('check_end_time','require','请填写查体结束时间!', self::MUST_VALIDATE),
        array('reserve_end_time','_validateCheckEndTime','查体结束时间不能小于查体开始时间!', self::EXISTS_VALIDATE, 'callback'),
        array('check_exclude_time','_validateCheckExcludeTime','查体时间填写错误，请检查!', self::EXISTS_VALIDATE, 'callback'),
        array('single_day_limit', 'number', '每天报名人数限制只能为数字'),
        array('chealth_project', '_validateChealthProject', '查体项目格式错误，请检查后再提交！', self::EXISTS_VALIDATE, 'callback')
    );

    protected $_auto = array(
        array('check_exclude_time', '_autoCheckExcludeTime', self::MODEL_BOTH, 'callback'),
        array('reserve_start_time', '_autoFormatTime', self::MODEL_BOTH, 'callback'),
        array('reserve_end_time', '_autoFormatTime', self::MODEL_BOTH, 'callback'),
        array('check_start_time', '_autoFormatTime', self::MODEL_BOTH, 'callback'),
        array('check_end_time', '_autoFormatTime', self::MODEL_BOTH, 'callback'),
        array('chealth_project', '_autoChealthProject', self::MODEL_BOTH, 'callback'),
        array('unit', '_autoUnit', self::MODEL_BOTH, 'callback'),
        array('status', '1', self::MODEL_INSERT),
        array('single_day_limit','intval', self::MODEL_BOTH,'function'),
        array('create_time','time', self::MODEL_INSERT,'function'),
        array('update_time','time', self::MODEL_UPDATE,'function')
    );

    /**
     * 添加或更新一个查体时间
     * @return bool
     */
    public function updateChealthTime(){
        if($this->create()){
            if(empty($this->data['id'])){
                $result = $this->add();
            }else{
                $result = $this->save();
            }
            if($result === false){
                return false;
            }
            return true;
        }else{
            return false;
        }
    }

    /**
     * 通过id获取一个查体的信息
     * @param $id
     * @return array
     */
    public function getInfoById($id){
        $info = $this->find($id);
        if(empty($info)){
            return array();
        }
        if(!empty($info['check_exclude_time'])){
            $info['check_exclude_time'] = explode(',', $info['check_exclude_time']);
        }
        if(!empty($info['chealth_project'])){
            $info['chealth_project'] = explode('|+|', $info['chealth_project']);
        }
        return $info;
    }

    /**
     * 获取预约报名项目列表
     * @param $map
     * @param $page
     * @param $order
     * @param $field
     * @param $r
     * @return array
     */
    public function getListByPage($map, $page, $order, $field, $r)
    {
        $totalCount = $this->where($map)->count();
        $list = array();
        if ($totalCount > 0) {
            $list = $this->where($map)->page($page, $r)->order($order)->field($field)->select();
            foreach($list as $k => $v){
                $list[$k]['check_exclude_time'] = explode(',', $v['check_exclude_time']);
                $list[$k]['chealth_project'] = explode('|+|', $v['chealth_project']);
            }
        }
        return array($list, $totalCount);
    }

    ######################### 自动验证处理部分 #########################

    /**
     * 验证不可查体时间格式是否正确
     * @param $data
     * @return bool
     */
    public function _validateCheckExcludeTime($data){
        if(I('type') == 1){
            for($i = 0, $len = count($data); $i < $len; $i += 2){
                if(empty($data[$i]) || empty($data[$i+1])){
                    return false;
                }
                if($data[$i] > $data[$i+1]){
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 验证预约开始时间是否大于结束时间,大于的话格式不正确
     * @param $data
     * @return bool
     */
    public function _validateReserveEndTime($data){
        $reserve_start_time = I('reserve_start_time');
        $reserve_end_time = I('reserve_end_time');
        if($reserve_start_time > $reserve_end_time){
            return false;
        }
        return true;
    }

    /**
     * 验证查体开始时间是否大于结束时间,大于的话格式不正确
     * @param $data
     * @return bool
     */
    public function _validateCheckEndTime($data){
        $check_start_time = I('check_start_time');
        $check_end_time = I('check_end_time');
        if($check_start_time > $check_end_time){
            return false;
        }
        return true;
    }

    /**
     * 验证查体项目是否有空
     * @param $data
     * @return bool
     */
    public function _validateChealthProject($data){
        foreach($data as $k => $v){
            if($k % 2 == 0 && empty($v)){
                return false;
            }
            if($k % 2 == 1 && $v !== '0' && empty($v)){
                return false;
            }
        }
        return true;
    }

    ######################### 自动完成处理部分 #########################

    /**
     * 自动处理单位的填写,未填写默认使用齐鲁工业大学
     * @param $data
     * @return string
     */
    public function _autoUnit($data){
        if(empty($data)){
            return '齐鲁工业大学';
        }else{
            return $data;
        }
    }

    /**
     * 自动处理不可查体时间为逗号分隔
     * @param $data
     * @return string
     */
    public function _autoCheckExcludeTime($data){
        if(empty($data)){
            return '';
        }else{
            if(I('type') == 1){
                foreach($data as $k => $v){
                    $data[$k] = strtotime(date('Y-m-d', $v));
                }
            }
            return implode(',', $data);
        }
    }

    /**
     * 自动处理查体项目为逗号分隔
     * @param $data
     * @return string
     */
    public function _autoChealthProject($data){
        if(empty($data)){
            return null;
        }else{
            return implode('|+|', $data);
        }
    }

    /**
     * 格式化时间到天数的0点
     * @param $data
     */
    public function _autoFormatTime($data){
        if(I('type') == 1){
            return strtotime(date('Y-m-d', $data));
        }
        return $data;
    }
}